package com.zkingsoft.actions.admin;

import java.io.UnsupportedEncodingException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.zkingsoft.actions.common.BaseController;
import com.zkingsoft.anotations.RemoveRequestToken;
import com.zkingsoft.anotations.SaveRequestToken;
import com.zkingsoft.model.TabClass;
import com.zkingsoft.model.Userinfo;
import com.zkingsoft.plugin.pojo.PaginationVO;
import com.zkingsoft.plugin.util.DateUtils;
import com.zkingsoft.plugin.util.ExcelUtil;
import com.zkingsoft.plugin.util.LogUtil;
import com.zkingsoft.plugin.util.ExcelUtil.ViewExcel;
import com.zkingsoft.pojo.AjaxResult;
import com.zkingsoft.services.UserinfoService;

/**
 * 员工管理
* @ClassName: StaffController 
* @Description:  
* @author 肖崇高  xiaochonggao@zkingsoft.com 
* @date 2016年11月17日 下午3:24:33 
*
 */
@Controller
@RequestMapping(value = "/admin/staff")
public class StaffController extends BaseController {

	@Resource
	private UserinfoService userInfoService;

	/**
	 * 分页查询员工信息
	* @Title: showList 
	* @Description: 
	* @param @param request
	* @param @param role
	* @param @param pageVo
	* @param @return    设定文件 
	* @return AjaxResult    返回类型 
	* @throws
	 */
	@RequestMapping(value = "/showList")
	public @ResponseBody AjaxResult showList(HttpServletRequest request, Userinfo staff, PaginationVO pageVo) {
		LogUtil.inof(getClass(), "员工信息:"+staff);
		return showList(userInfoService, staff, pageVo);
	}

	/**
	 * 新增或者修改员工信息
	* @Title: addOrModify 
	* @Description:  
	* @param @param role
	* @param @return    设定文件 
	* @return AjaxResult    返回类型 
	* @throws
	 */
	@RemoveRequestToken
	@RequestMapping(value = "/addOrModify")
	public @ResponseBody AjaxResult addOrModify(Userinfo staff) {
		if (staff.getuId() != null) {
			return modify(userInfoService, staff, "员工");
		} else {
			return add(userInfoService, staff, "员工");
		}
	}

	/**
	 * 删除员工信息
	* @Title: del 
	* @Description:  
	* @param @param keys
	* @param @return    设定文件 
	* @return AjaxResult    返回类型 
	* @throws
	 */
	@RequestMapping(value = "/del")
	public @ResponseBody AjaxResult del(String keys) {
		return remove(userInfoService, keys);
	}

	/**
	 * 进入员工编辑页面
	* @Title: editForm 
	* @Description:  
	* @param @param id
	* @param @param request
	* @param @return    设定文件 
	* @return String    返回类型 
	* @throws
	 */
	@SaveRequestToken
	@RequestMapping(value = "/editForm")
	public String editForm(Long id, HttpServletRequest request) {
		Userinfo staff;
		if (id != null) {
			staff = userInfoService.findById(id);
			request.setAttribute("obj", staff);
		}
		return "admin/staff-form";
	}

	/**
	 * 查詢所有的員工信息
	* @Title: all 
	* @Description:  
	* @param @return    设定文件 
	* @return AjaxResult    返回类型 
	* @throws
	 */
	@RequestMapping(value = "/all")
	public @ResponseBody AjaxResult all() {
		return findByModel(userInfoService, null);
	}
	
	/**
 	 * 
 	 * @Description:导出excel
 	 * @author:dingchuan
 	 * @return
 	 * 返回类型 ModelAndView
 	 * @date 2017年2月13日
 	 */
 	@RequestMapping(value = "/exportExcel")
 	public @ResponseBody ModelAndView report(ModelMap model, HttpServletRequest request, HttpServletResponse response,
 			Userinfo staff) {
 	    try {
 	        if (staff.getuName() != null && !(staff.getuName().equals(""))) {
 	        	staff.setuName(java.net.URLDecoder.decode(staff.getuName(), "UTF-8"));
 	        }
 	    } catch (UnsupportedEncodingException e1) {
 	        e1.printStackTrace();
 	    }
 	    // 查询符合条件的数据
 	    List<Userinfo> dataList = userInfoService.findByModel(staff);
 	    //把数据组装成我们要的数据，外面的list表示一行数据，也就是一个对象的信息，对应到excel的一行数据。里面的list表示对象的一个属性，对应excel的单元格
 	    SimpleDateFormat sdf = new SimpleDateFormat(DateUtils.DATE_FORMAT_DD);
 	    List<List< Object>> list = new ArrayList<>();
 	        for (Userinfo obj : dataList) {
 	            List<Object> temp = new ArrayList<>();
 	            temp.add(obj.getuStr1());
 	            temp.add(obj.getuName());
 	            temp.add(obj.getuSex());
 	            temp.add(obj.getuDepartment());
 	            temp.add(obj.getuJob());
 	            temp.add(obj.getuPhone());
 	            String dateStr = sdf.format(obj.getuJobdate());
 	            temp.add(dateStr);
 	            list.add(temp);
 	        }
 	         
 	        //调用我们的excel工具类，导出数据
 	    HSSFWorkbook workbook = ExcelUtil.generateExcel("员工列表",
 	            new String[] { "员工工号","员工姓名","性别","所属部门","职位","电话","入职时间"}, list);
 	    try {
 	        ViewExcel.buildExcel(null, workbook, request, response, "员工列表.xls");
 	    } catch (Exception e) {
 	        e.printStackTrace();
 	    }
 	    return new ModelAndView(new ViewExcel(), model);
 	}
	
}